/*
* Copyright (c) 2011 Mladen Milankovic <mmlado at gmail.com>.
*
* This file is part of Demine application
*
* Demine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Demine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with Demine.  If not, see <http://www.gnu.org/licenses/>.
*/

import QtQuick 1.0

Rectangle {
    id: table
    anchors.fill: parent
    color: '#3C7F3A'

    property int columns: 10
    property int rows: 10
    property int spacing: 5
    property int fieldSize: 80
    property int openedFields: 0
    property int numOfMines: 14
    property int flags: 0

    signal lost
    signal won
    signal firstOpen
    signal flag(int number)

    Grid {
        id: grid
        columns: table.columns
        rows: table.rows
        /*height:  (parent.width  / 10) * (rows * (1 + spacing) )
        width: (parent.width / 10) * (columns * (1 + spacing) )*/
        anchors.fill: parent
        spacing: table.spacing

        Repeater {
            model: table.columns * table.rows
            delegate: Field {
                width: table.fieldSize - spacing
                height: table.fieldSize - spacing
                onOpened: table.fieldOpened()
                onGameOver:  table.gameOver()
                onFlagged: {
                    table.flagged(status)
                }
            }
        }
        Component.onCompleted: {
            console.debug('finished');
        }
    }

    Component.onCompleted: {
        initialize();
        newGame();
    }

    function initialize() {
        for (var pos = 0; pos < (table.columns * table.rows); pos++) {
            var neighbors = Array();
            var c;
            var row;
            var cols;
            for (var y = -1; y <= 1; y++) {
                for (var x = -1; x <= 1; x++) {
                    if (!x && !y) continue;
                    cols = pos % table.columns;
                    row = (pos - (cols)) / table.columns;
                    if ((cols + x < 0) || (cols + x >= table.columns) || (row + y < 0) || (row + y >= table.rows)) continue;
                    c = grid.children[(row + y) * table.columns + cols + x];
                    if (c !== null) {
                        neighbors.push(c);
                    }
                }
            }
            grid.children[pos].setNeighbors(neighbors);
        }
    }

    function newGame() {
        openedFields = 0;
        flags = 0;
        for (var i = 0; i < (table.columns * table.rows); i++) {
            grid.children[i].initialize();
        }
        var position;
        for (var i = 0; i < numOfMines; i++) {
            position = Math.floor(Math.random()*(table.columns * table.rows));
            while (grid.children[position].mine) {
                position = Math.floor(Math.random()*(table.columns * table.rows));
            }
            grid.children[position].addMine(position);
        }
    }

    function fieldOpened() {
        if (!openedFields) table.firstOpen()
        openedFields++;
        if (openedFields == (table.columns * table.rows - table.numOfMines)) {
            table.won();
        }
    }

    function gameOver() {
        table.lost();
    }

    function flagged() {
        if (status) flags++;
        else flags--;
        table.flag(flags);
    }
}
